package com.huayi.server.Controller.app;

import com.huayi.server.Entity.UserEntity;
import com.huayi.server.Repository.UserRepostiory;
import com.huayi.server.Utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = "/user",method = RequestMethod.POST,consumes = "application/json")
public class UserController {

    @Autowired
    UserRepostiory repostiory;

    @Autowired
    Resp resp;

    /**
     * {
     "username":"hello",
     "password":"123456"
     }
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/register")
    @ResponseBody
    public Resp register(@RequestBody UserEntity userEntity){
        repostiory.save(userEntity);
        return resp;
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public Resp login(@RequestBody UserEntity userEntity){
        UserEntity userEntity1 = repostiory.findUserEntityByUsername(userEntity.getUsername());
        if (userEntity1 != null && userEntity1.getPassword().equals(userEntity.getPassword())){
            resp.setCode(200);
            resp.setDesc("登陆成功");
            resp.setData(userEntity1);
        }else{
            resp.setCode(-1);
            resp.setDesc("账号或密码错误");
        }
        return resp;
    }

}
